//
//  UIButton+Extension.swift
//  FZSina_Swift
//
//  Created by 李忠 on 7/19/16.
//  Copyright © 2016 FZLZ. All rights reserved.
//

import UIKit

extension UIButton{
    
    convenience init(title: String? = nil, titleColor: UIColor? = nil , imageNamed: String? = nil, backImageName: String? = nil, target:AnyObject?, action: Selector) {
        
        self.init()
        self.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        
        if let tit = title {
            self.setTitle(tit, forState: UIControlState.Normal)
        }
        if let titColor = titleColor {
            self.setTitleColor(titColor, forState: UIControlState.Normal)
        }
        
        
        
        if let img = imageNamed {
            self.setImage(UIImage(named: img), forState: UIControlState.Normal)
            self.setImage(UIImage(named: "\(img)_selected"), forState: UIControlState.Selected)
        }
        
        if let backIMG = backImageName {
            self.setBackgroundImage(UIImage(named: backIMG), forState: UIControlState.Normal)
            self.setBackgroundImage(UIImage(named: "\(backIMG)_selected"), forState: UIControlState.Selected)
        }
        
    }
    
    
    convenience init(target:AnyObject?, action: Selector, title: String? = nil, titleColor: UIColor? = nil , imageNamed: String? = nil, selectedIMG: String? = nil) {
        
        self.init()
        self.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        
        if let tit = title {
            self.setTitle(tit, forState: UIControlState.Normal)
            self.titleLabel?.font = UIFont.systemFontOfSize(13)
        }
        if let titColor = titleColor {
            self.setTitleColor(titColor, forState: UIControlState.Normal)
        }
        
        if let img = imageNamed {
            self.setImage(UIImage(named: img), forState: UIControlState.Normal)
        }
        if let slIMG = selectedIMG {
            self.setImage(UIImage(named: slIMG), forState: UIControlState.Selected)
        }
     }
    
}
